<?php
//入驻商管理

namespace Admin\Controller;
use Think\Controller;
class SecurityController extends CommonController {
    public function listc(){
        $listinfo = M('supplier')->select();

        $obj = M('supplier'); // 实例化User对象
        $count      = $obj->count();// 查询满足要求的总记录数
        $page = getpage($count,'15');
        $list = $obj->order('add_time')->limit($page->firstRow,$page->listRows)->select();
        $this->assign('listinfo',$list);// 赋值数据集
        $this->assign('page',$page->show());// 赋值分页输出
        $this->display(); // 输出模板

       
    }

    public function qr_listc(){
    	$listinfo = M('security')->select();
        //print_r($listinfo);
        $this->assign('listinfo',$listinfo);
        $this->display();
    	
    }

    public function add(){  //添加入驻商
        //获取省级地区

        $province=M('areas')->where(array('parent_id'=>1))->select();
        $this->assign('region',$province);

        $this->display();
    }


    public function do_add(){
        //入库
        if(IS_POST){
            //print_r($_POST);
            $po = I('post.');
            $data = array(
                'supplier_user' =>$po['supplier_user'],
                'supplier_pwd' =>md5($po['supplier_pwd']),
                'company_name' =>$po['company_name'],
                'province' =>$po['pro'],
                'city' =>$po['city'],
                'district' =>$po['area'],
                'address' =>$po['address'],
                'tel' =>$po['tel'],
                'email' =>$po['email'],
                'guimo' =>$po['guimo'],
                'company_type' =>$po['company_type'],
                'contacts_name' =>$po['contacts_name'],
                'contacts_phone' =>$po['contacts_phone'],
                'business_licence_number' =>$po['business_licence_number'],
                'business_licence_img' =>$po['face180'],
                'business_sphere' =>$po['business_sphere'],
                'idcard_front' =>$po['face1802'],
                'idcard_reverse' =>$po['face1803'],
                'id_card_no' =>$po['id_card_no'],
                'status' =>$po['status'],
                'add_time' =>time(),
                'dl_status' =>$po['dl_status'],
                );

            $oo = M('supplier')->add($data);
            //在security表中加入数据
            $arr = array(
                'supplier_id'=>$oo,
                'supplier_name' => $po['supplier_user'],
                 'company_name' =>$po['company_name'],
                );
            M('security')->add($arr);
            if($oo){
                $this->success('添加成功',U('listc')); 
            }
            
        }
    }




    //获取地区

    public function hqdq($id){
        $dq = M('areas')->where(array('area_id'=>$id))->find();
        return $dq;
    }

    public function getArea(){
        // $where['parent_id']=$_REQUEST['areaId'];
        // $area=M('areas')->where($where)->select();

        $parent_id['parent_id'] = I('post.pro_id','addslashes');
        $region = M('areas')->where($parent_id)->select();
        $opt = '<option>--请选择--</option>';
        foreach($region as $key=>$val){
            $opt .= "<option value='{$val['area_id']}'>{$val['area_name']}</option>";
        }
        echo json_encode($opt);


        // $this->ajaxReturn($area);
    }
    /**
     * 删除入驻商
     * @param  
     * @author wuzhaozhi
     * @return  
     */
    public function store_del()
    {
        $supplier_id = I('get.supplier_id');
        $goodsnum = M('goods')->where("supplier_id=".$supplier_id)->count();
        if($goodsnum)
        {
            
            die("<script>alert('该入驻商下已有产品，不能删除');history.back(-1);</script>");
        }
        $res = M('supplier')->where("supplier_id=".$supplier_id)->delete();
        if($res)
        {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    /**
     * 修改入驻商信息
     * @param  
     * @author wuzhaozhi
     * @return  
     */
    public function store_update()
    {
        //地区
        $province=M('areas')->where(array('parent_id'=>1))->select();
        $this->assign('region',$province);

        $supplier_id = I('get.supplier_id');
        $storeinfo = M('supplier')->where("supplier_id=".$supplier_id)->find();
        $storeinfo['cityn'] = M('areas')->where(array('area_id'=>$storeinfo['city']))->getField('area_name');
        $storeinfo['districtn'] = M('areas')->where(array('area_id'=>$storeinfo['district']))->getField('area_name');
        $this->assign('info',$storeinfo);
        $this->display();
    }
    /**
     * 修改入驻商信息  提交表单
     * @param  
     * @author wuzhaozhi
     * @return  
     */
    public function do_store_update()
    {
        $supplier_id = I('post.supplier_id');
        $po = I('post.');
        $arr = array(
                //'supplier_id'=>$oo,
                'supplier_name' => $po['supplier_user'],
                'company_name' =>$po['company_name'],
                );
        M('security')->where(array('supplier_id'=>$supplier_id))->save($arr);
        //echo M('security')->getLastSql();exit;
        if($po['face180']){
            $business_licence_img = $po['face180'];
        }else{
            $business_licence_img = $po['business_licence_img'];
        }
        if($po['face1802']){
            $idcard_front = $po['face1802'];
        }else{
            $idcard_front = $po['idcard_front'];
        }
        if($po['face1803']){
            $idcard_reverse = $po['face1803'];
        }else{
            $idcard_reverse = $po['idcard_reverse'];
        }
        $data = array(
                'supplier_user' =>$po['supplier_user'],
                'supplier_pwd' =>md5(trim($po['supplier_pwd'])),
                'company_name' =>$po['company_name'],
                'province' =>$po['pro'],
                'city' =>$po['city'],
                'district' =>$po['area'],
                'address' =>$po['address'],
                'tel' =>$po['tel'],
                'email' =>$po['email'],
                'guimo' =>$po['guimo'],
                'company_type' =>$po['company_type'],
                'contacts_name' =>$po['contacts_name'],
                'contacts_phone' =>$po['contacts_phone'],
                'business_licence_number' =>$po['business_licence_number'],
                'business_licence_img' =>$business_licence_img,
                'business_sphere' =>$po['business_sphere'],
                'idcard_front' =>$idcard_front,
                'idcard_reverse' =>$idcard_reverse,
                'id_card_no' =>$po['id_card_no'],
                'status' =>$po['status'],
               // 'add_time' =>time(),
                'dl_status' =>$po['dl_status'],
                );
        if(!$po['supplier_pwd'])
        {
            unset($data['supplier_pwd']);
        }

         $oo = M('supplier')->where(array('supplier_id'=>$supplier_id))->save($data);
        // echo M('supplier')->getLastSql();exit;
        if($oo!==false){
            $this->success('修改成功',U('listc')); 
        }
    }
}